package com.vidmt.api.common.jee;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import org.springframework.web.context.WebApplicationContext;

import com.thinkgem.jeesite.common.config.Global;
import com.vidmt.api.common.cache.MyCacheManager;

public class WebContextListener extends org.springframework.web.context.ContextLoaderListener {
	private static final String KEY_CONTEXT_DIR = "CONTEXT_DIR";

	@Override
	public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
		StringBuilder sb = new StringBuilder();
		sb.append("\r\n======================================================================\r\n");
		sb.append("\r\n    欢迎使用 " + Global.getConfig("productName") + "  - Powered By http://vidmt.com\r\n");
		sb.append("\r\n======================================================================\r\n");
		System.out.println(sb.toString());

		WebApplicationContext context = super.initWebApplicationContext(servletContext);
		MyCacheManager cacheManager = context.getBean(MyCacheManager.class);
		cacheManager.clearAll();
		return context;
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		String log4jdir = event.getServletContext().getRealPath("/");
		System.setProperty(KEY_CONTEXT_DIR, log4jdir);
		super.contextInitialized(event);
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		System.getProperties().remove(KEY_CONTEXT_DIR);
		super.contextDestroyed(event);
	}
}
